package com.fh.fruit.controller.bill;

import com.fh.controller.base.BaseController;
import com.fh.entity.Page;
import com.fh.fruit.service.bill.BillManager;
import com.fh.fruit.service.bill.ReconciliationManager;
import com.fh.fruit.util.DateUtil;
import com.fh.util.Jurisdiction;
import com.fh.util.ObjectExcelView;
import com.fh.util.PageData;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "reconciliation")
public class ReconciliationController extends BaseController {
	String menuUrl = "reconciliation/list.do"; //菜单地址(权限用)
	
    @Resource(name = "ReconciliationManager")
    private ReconciliationManager reconciliationManager;


    /**账单列表
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/list")
	public ModelAndView listGoods(Page page,HttpSession httpSession)throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		String num=pd.getString("num");
		if(num != null){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
			Date date = sdf.parse(DateUtil.getTime());
			pd.put("LongTerm",sdf.format(date));
		}
//		page.setShowCount(15);
		page.setPd(pd);
		List<Map<String,Object>> reconciliationList = reconciliationManager.listReconciliation(page);
		List<Map<String, Object>> count = reconciliationManager.reconciliationCount(page);
		long numPen = 0;
		String sumPrice = null;
		if(count.isEmpty()){
			numPen = 0;
			sumPrice = "0.00";
		}else{
			for(int i = 0; i < count.size(); i++){
				numPen = (long) count.get(i).get("numPen");
				sumPrice = (String) count.get(i).get("sumPrice");
			}
		}
		mv.addObject("numPen", numPen);
		mv.addObject("sumPrice", sumPrice);
		mv.setViewName("farm/bill/reconciliation_list");
		mv.addObject("reconciliationList", reconciliationList);
		mv.addObject("pd", pd);
		mv.addObject("QX",Jurisdiction.getHC());	//按钮权限
		return mv;
	}
	
	
	/**导出账单信息到EXCEL
	 * @return
	 */
	@RequestMapping(value="/excel")
	public ModelAndView exportExcel(Page page){
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		try{
			if(Jurisdiction.buttonJurisdiction(menuUrl, "cha")){
				Map<String,Object> dataMap = new HashMap<String,Object>();
				List<String> titles = new ArrayList<String>();
				titles.add("排序"); 		//1
				titles.add("交易编码");  		//2
				titles.add("第三方交易编码");			//3
				titles.add("用户手机号");			//4
				titles.add("支付方式");			//5
				titles.add("交易金额（元）");			//6
				titles.add("商品");			//7
				titles.add("支付时间");		//8
				dataMap.put("titles", titles);
				page.setShowCount(15);
				page.setCurrentPage(Integer.parseInt(pd.getString("pageNum")));
				page.setPd(pd);
				List<Map<String,Object>> reconciliationList = reconciliationManager.listReconciliationExcl(page);
				List<PageData> varList = new ArrayList<PageData>();
				for(int i=0;i<reconciliationList.size();i++){
					PageData vpd = new PageData();
					String pay_mode=(String)reconciliationList.get(i).get("pay_mode");
					String result_handel_state=(String)reconciliationList.get(i).get("result_handel_state");
					String finish_time=(String)reconciliationList.get(i).get("finish_time");
					if(pay_mode != null && !"".equals(pay_mode)){
						if("wechat".equals(pay_mode)){
							pay_mode="微信";
						}else{
							pay_mode="支付宝";
						}
					}else{
						pay_mode="非消费";
					}
					if(result_handel_state != null && !"".equals(result_handel_state)) {
						if("00".equals(result_handel_state)){
							result_handel_state="未对账";
						}else if("01".equals(result_handel_state)){
							result_handel_state="对账成功";
						}else{
							result_handel_state="对账失败";
						}
					}
					if(finish_time==null || "".equals(finish_time)){
						finish_time="未支付";
					}
					vpd.put("var1", (i+1));		//1
					vpd.put("var2", (String)reconciliationList.get(i).get("transaction_code"));		//2
					vpd.put("var3", (String)reconciliationList.get(i).get("tradeNo"));			//3
					vpd.put("var4", (String)reconciliationList.get(i).get("phone"));	//4
					vpd.put("var5", pay_mode);	//5
					vpd.put("var6", reconciliationList.get(i).get("price").toString());		//6
					vpd.put("var7", (String)reconciliationList.get(i).get("remark"));		//7
					vpd.put("var8", finish_time);	//8	
					varList.add(vpd);
				}
				dataMap.put("varList", varList);
				ObjectExcelView erv = new ObjectExcelView();					//执行excel操作
				mv = new ModelAndView(erv,dataMap);
			}
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
}
